const express = require('express')
const Router = express.Router()
const {getBrowserInstance} = require('./instance')

// http://localhost:3000/v1/test?id=1
Router.get('/test', function (req, res) {
	let id = req.query.id
	res.json({
		"hello": "world" + id
	});
})

// http://localhost:3000/v1/test2?phone=17357418051
Router.get('/test2', function (req, res) {
	(async (req, res) => {
        let phone = req.query.phone
		const url = `https://haoma.baidu.com/phoneSearch?search=${phone}`
        console.log("url: " + url)

        const browser = await getBrowserInstance()
        const page = await browser.newPage()
        page.on('response', async (response) => {
            const url = response.url()
            if (url.indexOf('/api/v1/search') > -1) {
                console.log(response.status())
                console.log('response.text()', await response.text())
                await res.send(await response.text())
            }
        })
        await page.goto(url, { waitUntil: 'networkidle0'})
//        await browser.close()

//        await res.json({
//            "code": 0,
//            "message": "success"
//        })
	})(req, res)
})

module.exports = Router
